import { computed, reactive, Ref, ref } from "vue";
import { formItemColumnMap } from "../data";
import { getBillCostsApi, getDepositByCustomerIdApi } from "@/api/financialManagement/receivable/statement";
export default function InitHook(state: { type: Ref<number> }) {
  const { type } = state;
  const customerAccountRef = ref<any>(null);
  const customerBillRef = ref<any>(null);
  const childXTable = ref<any>(null);
  const loading = ref<boolean>(false);
  const dialogOpts = reactive<Base.DialogOpts>({
    title: "",
    width: "520px",
    component: "",
    visible: false,
    type: 1,
    passData: {}
  });
  const billInfo = reactive<{ depositList: Array<Base.GeneraDataType>; costList: Array<Base.GeneraDataType>; sumUnChargeOffPrice: number; sumReceivablePrice: number }>({
    depositList: [],
    costList: [],
    sumReceivablePrice: 0,
    sumUnChargeOffPrice: 0
  });
  const searchForm = reactive<ReceivableStatement.BillCostrSearchForm>({
    pageNum: 1,
    pageSize: 999999,
    beforePageNum: 0,
    receivableBillIds: [],
    waybillNums: "",
    state: 0,
    channelId: [],
    currency: "",
    type: 0,
    costName: "",
    depositTime: [],
    customerId: []
  });
  const selectMap = ref<Base.GeneraDataType<string[]>>({});
  const formState = reactive<any>({});
  const formItemColumn = computed<Array<{ name: string; label: string }>>(() => {
    return formItemColumnMap[type.value];
  });
  const init = (passData: Ref<ReceivableStatement.ListRows[]>) => {
    loading.value = true;
    getDepositByCustomerIdApi({ customerId: passData.value[0].customerId })
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          billInfo.depositList = data || [];
        }
      })
      .finally(() => {
        loading.value = false;
      });
    getBillCostsApi({ pageNum: 1, pageSize: 999999, receivableBillIds: passData.value.map((t: any) => t.id) } as ReceivableStatement.BillCostsParams)
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          billInfo.costList = data.list || [];
          billInfo.sumReceivablePrice = data.sumReceivablePrice || 0;
          billInfo.sumUnChargeOffPrice = data.sumUnChargeOffPrice || 0;
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };
  // 剩余来款核销金额
  const selectDepositAmount = computed<number>(() => {
    if (!customerAccountRef.value) return 0;
    const selectAll = customerAccountRef.value.getCheckboxRecords().map((t: any) => t.unChargeOffPrice || 0);
    return selectAll
      .reduce((a: number, b: number) => {
        return (a * 100 + b * 100) / 100;
      }, 0)
      .toFixed(2);
  });
  // 已选未核销金额
  const selectUnchargeOff = computed<number>(() => {
    const result = [];
    for (const key in selectMap.value) {
      const source = selectMap.value[key].map((t: any) => t.unChargeOffPrice || 0);
      result.push(...source);
    }
    return result.reduce((a: number, b: number) => (a * 100 + b * 100) / 100, 0).toFixed(2);
  });
  // 已选运单
  const selectWaybillNum = computed<number>(() => {
    const result = [];
    console.log("ddddd", selectMap.value);
    for (const key in selectMap.value) {
      const source = selectMap.value[key].map((t: any) => t.waybillNum);
      result.push(...Array.from(new Set(source)));
    }
    return result.length;
  });

  const selectRef = ref([]) as any;
  return {
    billInfo,
    formState,
    formItemColumn,
    init,
    customerAccountRef,
    selectDepositAmount,
    searchForm,
    customerBillRef,
    loading,
    selectMap,
    selectUnchargeOff,
    selectWaybillNum,
    dialogOpts,
    childXTable,
    selectRef
  };
}
